package cn.fboost.mtos.controller;

import cn.fboost.mtos.annotation.RoleIs;
import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.dto.request.*;
import cn.fboost.mtos.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 修改用户资料
     *
     * @param request {@link UserProfileUpdateRequest}
     * @return {@link Response}
     */
    @PostMapping("profile/update")
    public Response updateUserProfile(@RequestBody @Validated UserProfileUpdateRequest request) {
        return userService.updateUserProfile(request);
    }

    /**
     * 列出用户列表 - 管理员接口
     *
     * @param request {@link PageRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("list")
    public Response userList(@RequestBody @Validated PageRequest request) {
        return userService.userList(request);
    }

    /**
     * 新增管理员 - 管理员接口
     *
     * @param request {@link AdminCreateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("admin/create")
    public Response createAdmin(@RequestBody @Validated AdminCreateRequest request) {
        return userService.createAdmin(request);
    }

    /**
     * 删除用户 - 管理员接口
     *
     * @param request {@link AminDeleteRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @DeleteMapping("admin/delete")
    public Response deleteAdmin(@RequestBody @Validated AminDeleteRequest request) {
        return userService.deleteAdmin(request);
    }

    /**
     * 修改管理员资料 - 管理员接口
     *
     * @param request {@link AdminProfileUpdateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("admin/profile/update")
    public Response updateAdminProfile(@RequestBody @Validated AdminProfileUpdateRequest request) {
        return userService.updateAdminProfile(request);
    }
}
